package com.sanpu.kxe.rxservice.client;

import android.support.v4.util.Pair;
import android.text.TextUtils;

import com.alibaba.fastjson.JSONObject;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.sanpu.kxe.business.repay.bean.AddRepayOutput;
import com.sanpu.kxe.business.repay.bean.AreaOutput;
import com.sanpu.kxe.business.repay.bean.MccListOutput;
import com.sanpu.kxe.business.repay.bean.QueryPlanDetailsOutput;
import com.sanpu.kxe.business.repay.bean.QueryPlanOutput;
import com.sanpu.kxe.business.repay.bean.RateDetailsOutput;
import com.sanpu.kxe.business.repay.bean.RefundPlanOutput;
import com.sanpu.kxe.business.repay.bean.RepayCreditOutput;
import com.sanpu.kxe.common.interfaces.CommonConfig;
import com.sanpu.kxe.common.interfaces.UrlConstants;
import com.sanpu.kxe.rxservice.BaseClient;
import com.sanpu.kxe.rxservice.MetaBean;
import com.sanpu.kxe.rxservice.RequestMap;
import com.sanpu.kxe.rxservice.interfaces.RepayService;

import java.util.List;

import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;
import rx.Observable;
import rx.functions.Func1;

/**
 * Description：代还模块接口
 * Created by 薛勇军 on 2019/4/10.
 */

public class RepayClient extends BaseClient {

    private static RepayClient mInstance;

    public static RepayClient getInstance() {
        if (mInstance == null) {
            synchronized (RepayClient.class) {
                if (mInstance == null) {
                    mInstance = new RepayClient();
                }
            }
        }
        return mInstance;
    }

    private RepayService service;

    private RepayClient() {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(UrlConstants.getBaseUrl())
                .client(client)
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .addConverterFactory(GsonConverterFactory.create())
                .build();

        service = retrofit.create(RepayService.class);
    }

    /**
     * 代还首页
     */
    public Observable<RepayCreditOutput> index(JSONObject reqData) {
        RequestMap requestMap = new RequestMap();
        requestMap.put(CommonConfig.REQDATA, new Gson().toJson(reqData));
        return callMapAfterLogin(service.index(requestMap.getReqParams(true)))
                .flatMap(new Func1<Pair<MetaBean, String>, Observable<RepayCreditOutput>>() {
                    @Override
                    public Observable<RepayCreditOutput> call(Pair<MetaBean, String> pair) {
                        if (pair == null) {
                            return toObservable(null);
                        }
                        RepayCreditOutput output = null;
                        if (!TextUtils.isEmpty(pair.second)) {
                            output = new Gson().fromJson(pair.second, new TypeToken<RepayCreditOutput>() {
                            }.getType());
                        }
                        return toObservable(new Pair<>(pair.first, output));
                    }
                });
    }

    /**
     * 解绑信用卡
     */
    public Observable<String> unbindCard(JSONObject reqData) {
        RequestMap requestMap = new RequestMap();
        requestMap.put(CommonConfig.REQDATA, new Gson().toJson(reqData));
        return callMapAfterLogin(service.unbindCard(requestMap.getReqParams(true)))
                .flatMap(new Func1<Pair<MetaBean, String>, Observable<String>>() {
                    @Override
                    public Observable<String> call(Pair<MetaBean, String> pair) {
                        return toObservable(pair);
                    }
                });
    }

    /**
     * 绑定信用卡
     */
    public Observable<AddRepayOutput> bindCard(JSONObject reqData) {
        RequestMap requestMap = new RequestMap();
        requestMap.put(CommonConfig.REQDATA, new Gson().toJson(reqData));
        return callMapAfterLogin(service.bindCard(requestMap.getReqParams(true)))
                .flatMap(new Func1<Pair<MetaBean, String>, Observable<AddRepayOutput>>() {
                    @Override
                    public Observable<AddRepayOutput> call(Pair<MetaBean, String> pair) {
                        if (pair == null) {
                            return toObservable(null);
                        }
                        AddRepayOutput output = null;
                        if (!TextUtils.isEmpty(pair.second)) {
                            output = new Gson().fromJson(pair.second, new TypeToken<AddRepayOutput>() {
                            }.getType());
                        }
                        return toObservable(new Pair<>(pair.first, output));
                    }
                });
    }

    /**
     * 获取费率列表
     */
    public Observable<List<RateDetailsOutput>> repayFeeList() {
        return callMapAfterLogin(service.repayFeeList(new RequestMap().getReqParams(true)))
                .flatMap(new Func1<Pair<MetaBean, String>, Observable<List<RateDetailsOutput>>>() {
                    @Override
                    public Observable<List<RateDetailsOutput>> call(Pair<MetaBean, String> pair) {
                        if (pair == null) {
                            return toObservable(null);
                        }
                        List<RateDetailsOutput> output = null;
                        if (!TextUtils.isEmpty(pair.second)) {
                            output = new Gson().fromJson(pair.second, new TypeToken<List<RateDetailsOutput>>() {
                            }.getType());
                        }
                        return toObservable(new Pair<>(pair.first, output));
                    }
                });
    }

    /**
     * 卡片计划列表
     */
    public Observable<List<QueryPlanOutput>> cardPlanList(JSONObject reqData) {
        RequestMap requestMap = new RequestMap();
        requestMap.put(CommonConfig.REQDATA, new Gson().toJson(reqData));
        return callMapAfterLogin(service.cardPlanList(requestMap.getReqParams(true)))
                .flatMap(new Func1<Pair<MetaBean, String>, Observable<List<QueryPlanOutput>>>() {
                    @Override
                    public Observable<List<QueryPlanOutput>> call(Pair<MetaBean, String> pair) {
                        if (pair == null) {
                            return toObservable(null);
                        }
                        List<QueryPlanOutput> outputs = null;
                        if (!TextUtils.isEmpty(pair.second)) {
                            outputs = new Gson().fromJson(pair.second, new TypeToken<List<QueryPlanOutput>>() {
                            }.getType());
                        }
                        return toObservable(new Pair<>(pair.first, outputs));
                    }
                });
    }

    /**
     * 通过状态查询卡片计划列表
     */
    public Observable<List<QueryPlanOutput>> cardPlanListForStatus(JSONObject reqData) {
        RequestMap requestMap = new RequestMap();
        requestMap.put(CommonConfig.REQDATA, new Gson().toJson(reqData));
        return callMapAfterLogin(service.cardPlanListForStatus(requestMap.getReqParams(true)))
                .flatMap(new Func1<Pair<MetaBean, String>, Observable<List<QueryPlanOutput>>>() {
                    @Override
                    public Observable<List<QueryPlanOutput>> call(Pair<MetaBean, String> pair) {
                        if (pair == null) {
                            return toObservable(null);
                        }
                        List<QueryPlanOutput> outputs = null;
                        if (!TextUtils.isEmpty(pair.second)) {
                            outputs = new Gson().fromJson(pair.second, new TypeToken<List<QueryPlanOutput>>() {
                            }.getType());
                        }
                        return toObservable(new Pair<>(pair.first, outputs));
                    }
                });
    }

    /**
     * 卡片计划明细列表
     */
    public Observable<QueryPlanDetailsOutput> cardPlanDetailList(JSONObject reqData) {
        RequestMap requestMap = new RequestMap();
        requestMap.put(CommonConfig.REQDATA, new Gson().toJson(reqData));
        return callMapAfterLogin(service.cardPlanDetailList(requestMap.getReqParams(true)))
                .flatMap(new Func1<Pair<MetaBean, String>, Observable<QueryPlanDetailsOutput>>() {
                    @Override
                    public Observable<QueryPlanDetailsOutput> call(Pair<MetaBean, String> pair) {
                        if (pair == null) {
                            return toObservable(null);
                        }
                        QueryPlanDetailsOutput output = null;
                        if (!TextUtils.isEmpty(pair.second)) {
                            output = new Gson().fromJson(pair.second, new TypeToken<QueryPlanDetailsOutput>() {
                            }.getType());
                        }
                        return toObservable(new Pair<>(pair.first, output));
                    }
                });
    }

    /**
     * 取消计划
     */
    public Observable<String> cancelPlan(JSONObject reqData) {
        RequestMap requestMap = new RequestMap();
        requestMap.put(CommonConfig.REQDATA, new Gson().toJson(reqData));
        return callMapAfterLogin(service.cancelPlan(requestMap.getReqParams(true)))
                .flatMap(new Func1<Pair<MetaBean, String>, Observable<String>>() {
                    @Override
                    public Observable<String> call(Pair<MetaBean, String> pair) {
                        return toObservable(pair);
                    }
                });
    }

    /**
     * 生成计划
     */
    public Observable<RefundPlanOutput> createPlan(JSONObject reqData) {
        RequestMap requestMap = new RequestMap();
        requestMap.put(CommonConfig.REQDATA, new Gson().toJson(reqData));
        return callMapAfterLogin(service.createPlan(requestMap.getReqParams(true)))
                .flatMap(new Func1<Pair<MetaBean, String>, Observable<RefundPlanOutput>>() {
                    @Override
                    public Observable<RefundPlanOutput> call(Pair<MetaBean, String> pair) {
                        if (pair == null) {
                            return toObservable(null);
                        }
                        RefundPlanOutput output = null;
                        if (!TextUtils.isEmpty(pair.second)) {
                            output = new Gson().fromJson(pair.second, new TypeToken<RefundPlanOutput>() {
                            }.getType());
                        }
                        return toObservable(new Pair<>(pair.first, output));
                    }
                });
    }

    /**
     * 修改计划还款天数
     */
    public Observable<RefundPlanOutput> updatePlanPaymentDays(JSONObject reqData) {
        RequestMap requestMap = new RequestMap();
        requestMap.put(CommonConfig.REQDATA, new Gson().toJson(reqData));
        return callMapAfterLogin(service.updatePlanPaymentDays(requestMap.getReqParams(true)))
                .flatMap(new Func1<Pair<MetaBean, String>, Observable<RefundPlanOutput>>() {
                    @Override
                    public Observable<RefundPlanOutput> call(Pair<MetaBean, String> pair) {
                        if (pair == null) {
                            return toObservable(null);
                        }
                        RefundPlanOutput output = null;
                        if (!TextUtils.isEmpty(pair.second)) {
                            output = new Gson().fromJson(pair.second, new TypeToken<RefundPlanOutput>() {
                            }.getType());
                        }
                        return toObservable(new Pair<>(pair.first, output));
                    }
                });
    }

    /**
     * 获取行业列表
     */
    public Observable<List<MccListOutput>> getMccList() {
        return callMapAfterLogin(service.getMccList(new RequestMap().getReqParams(true)))
                .flatMap(new Func1<Pair<MetaBean, String>, Observable<List<MccListOutput>>>() {
                    @Override
                    public Observable<List<MccListOutput>> call(Pair<MetaBean, String> pair) {
                        if (pair == null) {
                            return toObservable(null);
                        }
                        List<MccListOutput> outputs = null;
                        if (!TextUtils.isEmpty(pair.second)) {
                            outputs = new Gson().fromJson(pair.second, new TypeToken<List<MccListOutput>>() {
                            }.getType());
                        }
                        return toObservable(new Pair<>(pair.first, outputs));
                    }
                });
    }

    /**
     * 执行计划
     */
    public Observable<String> executePlan(JSONObject reqData) {
        RequestMap requestMap = new RequestMap();
        requestMap.put(CommonConfig.REQDATA, new Gson().toJson(reqData));
        return callMapAfterLogin(service.executePlan(requestMap.getReqParams(true)))
                .flatMap(new Func1<Pair<MetaBean, String>, Observable<String>>() {
                    @Override
                    public Observable<String> call(Pair<MetaBean, String> pair) {
                        return toObservable(pair);
                    }
                });
    }

    /**
     * 修改卡信息
     */
    public Observable<String> updateCard(JSONObject reqData) {
        RequestMap requestMap = new RequestMap();
        requestMap.put(CommonConfig.REQDATA, new Gson().toJson(reqData));
        return callMapAfterLogin(service.updateCard(requestMap.getReqParams(true)))
                .flatMap(new Func1<Pair<MetaBean, String>, Observable<String>>() {
                    @Override
                    public Observable<String> call(Pair<MetaBean, String> pair) {
                        return toObservable(pair);
                    }
                });
    }

    /**
     * 修改卡信息
     */
    public Observable<String> updateCreditBank(JSONObject reqData) {
        RequestMap requestMap = new RequestMap();
        requestMap.put(CommonConfig.REQDATA, new Gson().toJson(reqData));
        return callMapAfterLogin(service.updateCreditBank(requestMap.getReqParams(true)))
                .flatMap(new Func1<Pair<MetaBean, String>, Observable<String>>() {
                    @Override
                    public Observable<String> call(Pair<MetaBean, String> pair) {
                        return toObservable(pair);
                    }
                });
    }

    /**
     * 获取省市区
     */
    public Observable<AreaOutput> provinces() {
        return callMap(service.provinces(new RequestMap().getReqParams(false)))
                .flatMap(new Func1<Pair<MetaBean, JSONObject>, Observable<AreaOutput>>() {
                    @Override
                    public Observable<AreaOutput> call(Pair<MetaBean, JSONObject> pair) {
                        if (pair == null) {
                            return toObservable(null);
                        }
                        AreaOutput output = null;
                        MetaBean metaBean = new MetaBean();
                        try {
                            JSONObject jsonObject = pair.second;
                            metaBean.setRespCode(jsonObject.getString("respCode"));
                            metaBean.setRespMsg(jsonObject.getString("respMsg"));
                            output = new Gson().fromJson(jsonObject.getString("respData"),
                                    new TypeToken<AreaOutput>() {}.getType());
                        } catch (Exception e) {
                            e.printStackTrace();
                        }

                        return toObservable(new Pair<>(metaBean, output));
                    }
                });
    }
}
